package me.jiatao.baselibrary.widgets

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import me.jiatao.baselibrary.R

/**
 *   加载对话框封装
 */
class ProgressLoading private constructor(context: Context, theme: Int) : Dialog(context, theme) {

    companion object {
        private lateinit var mDialog: ProgressLoading
        private lateinit var mLoadingView: LVCircular
        /**
         *   创建加载对话框
         */
        fun create(context: Context): ProgressLoading {
            //样式引入
            mDialog = ProgressLoading(context, R.style.LightProgressDialog)
            //设置布局
            mDialog.setContentView(R.layout.progress_dialog)
            mDialog.setCancelable(true)
            mDialog.setCanceledOnTouchOutside(false)
            mDialog.window!!.attributes.gravity = Gravity.CENTER

            val lp = mDialog.window!!.attributes
            lp.dimAmount = 0.2f
            //设置属性
            mDialog.window!!.attributes = lp

            //获取动画视图
            mLoadingView = mDialog.findViewById(R.id.progress_dialog_loading)

            return mDialog
        }
    }

    /**
     *   显示加载对话框，动画开始
     */
    fun showLoading() {
        super.show()
        mLoadingView.startAnim()
    }

    /**
     *   隐藏加载对话框，动画停止
     */
    fun hideLoading() {
        super.dismiss()
        mLoadingView.stopAnim()
    }


    override fun onWindowFocusChanged(hasWindowFocus: Boolean) {
        super.onWindowFocusChanged(hasWindowFocus)
        if (hasWindowFocus) {
            if (isShowing) {
                mLoadingView.startAnim()
            }
        }
        else{
            if (!isShowing) {
                mLoadingView.stopAnim()
            }
        }
    }

}
